package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.CommoditBO;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.CommoditDao;
import com.jackiechancinema.ssm.dto.CommoReDTO;
import com.jackiechancinema.ssm.dto.CommoditDetailDTO;
import com.jackiechancinema.ssm.dto.CommoditExcelDTO;
import com.jackiechancinema.ssm.dto.OrderExcelDto;
import com.jackiechancinema.ssm.entity.Commodit;
import com.jackiechancinema.ssm.entity.CommoditHead;
import com.jackiechancinema.ssm.entity.Order;
import com.jackiechancinema.ssm.listener.CommoditReadListener;
import com.jackiechancinema.ssm.service.Inte.CommoditService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class CommoditServiceImpl implements CommoditService {
    @Autowired
    private CommoditDao commoditDao;
    @Autowired
    private CommoditReadListener listener;

    @Override
    public PageVo<Commodit> queryPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        //2.获取分页的查询结果
        PageInfo<Commodit> pageInfo=new PageInfo<>(commoditDao.selectAll());
        return new PageVo<Commodit>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }



    @Override
    public PageVo<CommoditDetailDTO> queryPageDetail(int page, int limit) {
        PageHelper.startPage(page, limit);
        //2.获取分页的查询结果
        PageInfo<CommoditDetailDTO> pageInfo=new PageInfo<>(commoditDao.selectCommoditDetail());
        System.out.println(pageInfo.getList());

        return new PageVo<CommoditDetailDTO>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R update(Commodit commodit) {

        if(commoditDao.update(commodit)>0){
            return R.ok("OK");
        }else {
            return R.fail();
        }
    }

    @Override
    public R save(Commodit commodit) {
        if(commoditDao.insertCommodit(commodit)>0){
            return R.ok("OK");
        }else {
            return R.fail();
        }
    }

    @Override
    public R delete(int commoditId) {
        int num = commoditDao.deleteById(commoditId);
        if(num>0){
            return R.ok("删除成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public PageVo<CommoReDTO> queryRecording(int page, int limit) {
        PageHelper.startPage(page,limit);

        PageInfo<CommoReDTO> pageInfo = new PageInfo<>(commoditDao.allRecording());

        return new PageVo<CommoReDTO>(SystemContant.PAGE_R,"ok",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public PageVo<CommoditHead> checkHead(int page, int limit) {

        PageHelper.startPage(page, limit);
        //2.获取分页的查询结果
        PageInfo<CommoditHead> pageInfo=new PageInfo<>(commoditDao.allHead());

        return new PageVo<CommoditHead>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R headAdd(CommoditHead commoditHead) {
        int num = commoditDao.insertHead(commoditHead);
        if(num>0){
            return R.ok("添加成功");
        }else{
            return R.fail();
        }
    }
    //修改负责人
    @Override
    public R updateHead(CommoditHead commoditHead) {
        int num = commoditDao.updateHead(commoditHead);
        if(num>0){
            return R.ok("修改成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public R deleteHead(int commoditHeadId) {
        int num = commoditDao.deleteHead(commoditHeadId);
        if(num>0){
            return R.ok("删除成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public PageVo<Commodit> page(CommoditBO bo) {

        if(bo!=null){
            //1.开启分页插件
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            //2.获取分页的查询结果
            PageInfo<Commodit> pageInfo=new PageInfo<>(commoditDao.queryAllPlus(bo));
            //3.返回结果
            return new PageVo<Commodit>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Commodit>(SystemContant.PAGE_R,"OK",0,null);
    }

    @Override
    public R batchadd(MultipartFile file) {
        if(!file.isEmpty()){
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), CommoditExcelDTO.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();

    }

    @Override
    public void exportExcel(CommoditBO bo, HttpServletResponse response) {
        String fn = System.currentTimeMillis()+".xlsx";


        response.setHeader("content-disposition","attachment;filename="+fn);

        //3.获取要下载的数据
        List<Commodit> list=commoditDao.queryAllPlus(bo);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Order.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
